﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheckTTS
{
    class SpanTagCheckHandle : CheckHandle
    {
        private CheckHandle mSuccessor = null;
       // private const string CHECK_SPAN_EXPRESS = "<span(?=\\s)(?!(?:[^>\"\\']|\"[^\"]*\"|\\'[^\\']*\\')*?(?<=\\s)(?:term|range)\\s*=)(?!\\s*/?>)\\s+(?:\".*?\"|\\'.*?\\'|[^>]*?)+>";
        public const string CHECK_SPAN_EXPRESS = "(<span\\s+[^</]*>)|(</span>)";
        public MainForm mForm = null;

       public  SpanTagCheckHandle(MainForm form) {
            this.mForm = form;
        }

        public void checkHandle(string content, List<ErrorInfo> errorList, Stack<TagInfo> tagInfoStack, RichTextBox richTextBox)
        {
            matchSpanHead(content, errorList, tagInfoStack, richTextBox);

           if (mSuccessor !=null) {
                mSuccessor.checkHandle(content,errorList,tagInfoStack,richTextBox);
            }
        }

        public void setSuccessor(CheckHandle successor)
        {
            mSuccessor = successor;
        }

        private void matchSpanHead(string content,List<ErrorInfo> errorList, Stack<TagInfo> mStackTag,RichTextBox richTextBox) {
            MatchCollection matchCollection = Regex.Matches(content, CHECK_SPAN_EXPRESS); //满足pattern的匹配集合 
            foreach (Match match in matchCollection)
            {
                if ("</span>".Equals(match.Value))
                {
                    if (mStackTag.Count == 0)
                    {
                        ErrorInfo errorInfo = new ErrorInfo();
                        errorInfo.index = match.Index;
                        richTextBox.Select(match.Index, 7);
                        Console.WriteLine("匹配的位置："+match.Index);
                        richTextBox.SelectionBackColor = Color.Red;
                        errorInfo.errorInfo = "缺少<span >标签";
                        errorList.Add(errorInfo);
                        //Console.WriteLine("第" + (i + 1) + "行出错，" + errorInfo.errorInfo);
                    }
                    else
                    {
                        mStackTag.Pop();
                    }
                }
                else if (match.Value.Contains("<span"))
                {
                    TagInfo tagInfo = new TagInfo();
                    tagInfo.index = match.Index;
                    tagInfo.tagHeader = match.Value;
                    mStackTag.Push(tagInfo);
                }
            }//end of foreach

            while (mStackTag.Count > 0)
            {
                ErrorInfo errorInfo = new ErrorInfo();
                errorInfo.index = mStackTag.Pop().index;
                Console.WriteLine("匹配的位置：" + errorInfo.index);
                errorInfo.errorInfo = "缺少</span>标签";
                errorList.Add(errorInfo);
                richTextBox.Select(errorInfo.index, 5);
                richTextBox.SelectionBackColor = Color.Red;
                //Console.WriteLine("第" + (errorInfo.line) + "行出错，" + errorInfo.errorInfo);
            }
        }

    }
}
